﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace praca_v2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }  
        public void GetDataFromDb(string dbTableName, string connectionString = "ConnectionString")
        {
            {
                ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings[connectionString];
                SqlConnection connection = new SqlConnection(settings.ConnectionString);
                string query = "SELECT * FROM " + dbTableName;
                try
                {
                    connection.Open();
                    SqlCommand com1 = new SqlCommand(query, connection);
                    SqlDataReader dr = com1.ExecuteReader();
                    while (dr.Read())
                    {
                        dodajNew(dr["ID"].ToString(), dr["Email"].ToString(), dr["Haslo"].ToString(), dr["Imie"].ToString(), dr["Nazwisko"].ToString());

                    }


                }
                catch (Exception ex) { }
                finally { connection.Close(); }
            }
        }
        public void dodajNew(String id, String email, String haslo, String imie, String nazwisko)
        {
            dodajKomorke(id);
            dodajKomorke(email);
            dodajKomorke(haslo);
            dodajKomorke(imie);
            dodajKomorke(nazwisko);
        }
        public void dodajKomorke(String trescKom)
        {
            TableRow wiersz = new TableRow();
            TableCell komorka = new TableCell();
            komorka.Text = trescKom;
            wiersz.Cells.Add(komorka);
            Klienci.Rows.Add(wiersz);
        }

        protected void ButtonOdczyt_Click(object sender, EventArgs e)
        {
            //Test2("Klienci", TextBox1id.Text);
        }
        private void Test2(string dbTableName, string szukany, string connectionString = "ConnectionString")
        {
            ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings[connectionString];
            SqlConnection connection = new SqlConnection(settings.ConnectionString);
            connection.Open();

            int licznik = 0;

            SqlCommand liczbaWierszy = new SqlCommand("SELECT count(*) as ilosc FROM " + dbTableName, connection);
            int iloscWierszy = Convert.ToInt32(liczbaWierszy.ExecuteScalar());
            string[] tabId = new string[iloscWierszy];
            string[] tabEmail = new string[iloscWierszy];
            string[] tabHaslo = new string[iloscWierszy];
            string[] tabImie = new string[iloscWierszy];
            string[] tabNazwisko = new string[iloscWierszy];
            string[] tabNumer = new string[iloscWierszy];

            SqlCommand command = new SqlCommand("SELECT * FROM " + dbTableName, connection);
            SqlDataReader dr = command.ExecuteReader();
            while (dr.Read())
            {
                tabId[licznik] = dr["ID"].ToString();
                tabEmail[licznik] = dr["Email"].ToString();
                tabHaslo[licznik] = dr["Haslo"].ToString();
                tabImie[licznik] = dr["Imie"].ToString();
                tabNazwisko[licznik] = dr["Nazwisko"].ToString();

                licznik++;
            }
            connection.Close();

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            GetDataFromDb("Klienci");
        }

        protected void ButtonLogowanie_Click(object sender, EventArgs e)
        {
            Response.Redirect("Logowanie.aspx");
        }

        protected void ButtonRejestracja_Click(object sender, EventArgs e)
        {
            Response.Redirect("Rejestracja.aspx");
        }

        protected void ButtonOdzyskiwanie_Click(object sender, EventArgs e)
        {
            Response.Redirect("Odzyskiwanie.aspx");
        }




    }
}